﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using RobotCtrl;

namespace RobotView
{
    //Wenn Usercontrol auf Form Platziert ist, kommt event 
    // von Usercontrol auf Form nicht an!!! Event muss man schmeissen
    public partial class ConsoleView : UserControl
    {
        private Lamp[] lamps;
        private Switch[] switches;
        private RobotConsole console;

        public ConsoleView()
        {
            InitializeComponent();
            lamps = new Lamp[]{ lamp0, lamp1, lamp2, lamp3 };
            switches = new Switch[] { switch0, switch1, switch2, switch3 };
            foreach (Switch s in switches)
            {
                s.SwitchChanged +=new SwitchChange(switch_Changed);
            }
        }

        public ConsoleView(RobotConsole console)
            : this()
        {
            this.console = console;

            //System.windows.Forms TImer darf GUI Objekte verändern
            // andere Timer dürfen das nicht
            Timer timer = new Timer();
            timer.Interval = 200;
            timer.Tick += new EventHandler(timer_Tick);
            timer.Enabled = true;
        }

        //Status aller Schalter holen und darstellen
        private void timer_Tick(object sender, EventArgs e)
        {
            for (int i = 0; i < lamps.Length; i++)
            {
                lamps[i].State = console.Lamps[i];
            }
            for (int i = 0; i < switches.Length; i++)
            {
                switches[i].State = console.Switches[i];
            }
        }

        //Status der Schalter
        private void switch_Changed(object sender, EventArgs e)
        {
            for (int i = 0; i < switches.Length; i++)
            {
                if (switches[i].Equals((Switch)sender))
                {
                    //State invertieren
                    console.Switches[i] = !((Switch)sender).State;
                }
            }
        }

    }
}
